/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.chartextensions.customscale;

import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.JChartMenu.ChartMenuListeners.LaunchSetScaleDialogListener;
import JFreeChartExtensions.JChartMenu.JChartMenu;
import JFreeChartExtensions.chartextensions.AbstractStorableChartPanelExtension;
import JFreeChartExtensions.storable.StorableExtenstionObject;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;

/**
 *
 * @author Nobody
 */
public class CustomScaleExtension extends AbstractStorableChartPanelExtension {

    private JChartMenu setScaleMenuItem;

    public CustomScaleExtension(ExtendableChartPanel panel) {
        super(panel);
        name = "CustomScaleExtension";
    }

    @Override
    public void applyExtension(ChartPanel chartPanel) {
        this.chartPanel = (ExtendableChartPanel) chartPanel;
        setScaleMenuItem = new JChartMenu("Set scale dialog");
        setScaleMenuItem.addListener(new LaunchSetScaleDialogListener((ExtendableChartPanel) chartPanel));
        chartPanel.getPopupMenu().add(setScaleMenuItem);

    }

    @Override
    public void removeExtension() {
        chartPanel.getPopupMenu().remove(setScaleMenuItem);
    }

    @Override
    public void loadStoredExtension(StorableExtenstionObject extenstionObject) {
        Object[] objects = extenstionObject.getSerializedObjects();
        ValueAxis domainAxis = (ValueAxis) objects[0];
        String domainAxisLabel = (String) objects[1];
        ValueAxis rangeAxis = (ValueAxis) objects[2];
        String rangeAxisLabel = (String) objects[3];
        ValueAxis range2Axis = (ValueAxis) objects[4];
        String range2AxisLabel = (String) objects[5];


        if (domainAxis instanceof LogarithmicAxis) {
            chartPanel.setDomainAxisAndNotifyListeners(0, new LogarithmicAxis(domainAxisLabel));
            chartPanel.getXYPlot().setDomainAxis(new LogarithmicAxis(domainAxisLabel));
        } else {
            chartPanel.setDomainAxisAndNotifyListeners(0, new NumberAxis(domainAxisLabel));

        }
        if (rangeAxis instanceof LogarithmicAxis) {
            chartPanel.setRangeAxisAndNotifyListeners(0, new LogarithmicAxis(rangeAxisLabel));
        } else {
            chartPanel.setRangeAxisAndNotifyListeners(0, new NumberAxis(rangeAxisLabel));

        }
        if (range2Axis == null) {
            return;
        }
        if (range2Axis instanceof LogarithmicAxis) {
            chartPanel.setRangeAxisAndNotifyListeners(1, new LogarithmicAxis(range2AxisLabel));
        } else {
            chartPanel.setRangeAxisAndNotifyListeners(1, new NumberAxis(range2AxisLabel));
        }
    }

    @Override
    public StorableExtenstionObject storeExtension() {
        Object[] objectMap = new Object[6];
        XYPlot plot = chartPanel.getXYPlot();
        objectMap[0] = plot.getDomainAxis();
        objectMap[1] = plot.getDomainAxis().getLabel();

        objectMap[2] = plot.getRangeAxis();
        objectMap[3] = plot.getRangeAxis().getLabel();

        objectMap[4] = plot.getRangeAxis(1);
        if (plot.getRangeAxis(1) != null) {
            objectMap[5] = chartPanel.getXYPlot().getRangeAxis(1).getLabel();
        }


        return new StorableExtenstionObject(objectMap, name);
    }
}
